import { ElMessage } from "element-plus";
import dayjs from "dayjs";
import {
  cumulate,
  arrange,
  summary,
  policyCompute,
  stepCut,
} from "@/views/FlowStatus/utils";

import fetchSummaryData from "./fetchSummaryData.js";
import fetchHourlyData from "./fetchHourlyData.js";

export default function (replyTMA) {
  const begTime = replyTMA.begTime;
  const version = replyTMA.version;
  const tagsTerminal = replyTMA.tags;
  const rawTerminalData = replyTMA.rawData;
  const rawTerminalPlanData = replyTMA.rawDataOld;
  const rawSector = replyTMA.sector;

  if (!rawTerminalData || rawTerminalData.length == 0) {
    ElMessage.warning("出错了，未能检索到数据(⊙o⊙)…");
    return {};
  }

  const cumulatedTerminalData = cumulate(rawTerminalData);
  const cumulatedTerminalDataPlan = cumulate(rawTerminalPlanData);

  const targetDate = dayjs(begTime).format("YYYY-MM-DD");

  const arrangedTerminalData = policyCompute(
    arrange(
      cumulatedTerminalData,
      tagsTerminal,
      begTime,
      4,
      ["airport", "type", "point"],
      "YYYY-MM-DD HH:mm:ss"
    )
  );
  const arrangedTerminalPlanData = policyCompute(
    arrange(cumulatedTerminalDataPlan, tagsTerminal, begTime, 4, [
      "airport",
      "type",
      "point",
    ])
  );

  const summaryTerminalData = summary(cumulatedTerminalData, tagsTerminal, [
    "ZUUU",
    "ZUTF",
    "ZUMY",
    "DEP",
    "ARR",
  ]);
  const summaryTerminalPlanData = summary(
    cumulatedTerminalDataPlan,
    tagsTerminal,
    ["ZUUU", "ZUTF", "ZUMY", "DEP", "ARR"]
  );

  const isAfter = dayjs().isAfter(dayjs(begTime).add(24, "hour"));

  const icaoCodes = Object.keys(summaryTerminalData.airport);
  const tablesummaryTerminalData = fetchSummaryData({
    summaryTerminalData,
    summaryTerminalPlanData,
    icaoCodes,
    isAfter,
  });

  const { tableHourlyHeader, tableHourlyData } = fetchHourlyData({
    arrangedTerminalData,
    arrangedTerminalPlanData,
    icaoCodes,
    isAfter,
  });

  // Real Sector Count

  const realSectorList = stepCut(rawSector, 4);
  const realIpiaList = stepCut(fetchIpia(rawTerminalData), 4);

  // const summaryTerminalData = createsummaryTerminalData(
  //   summaryTerminalData,
  //   summaryTerminalPlanData,
  //   isAfter
  // );
  const tableData = transposeMatrixData(
    createTableData(
      arrangedTerminalData,
      arrangedTerminalPlanData,
      realSectorList,
      realIpiaList
    )
  );
  // const originData = transposeMatrixData(createOriginData(arrangedTerminalData));

  const output = {
    targetDate,
    isAfter,
    version,
    arrangedTerminalData,
    arrangedTerminalPlanData,
    summaryTerminalData,
    summaryTerminalPlanData,
    tableData,
    // summaryTerminalData,
    // originData,
    tablesummaryTerminalData,
    tableHourlyHeader,
    tableHourlyData,
  };

  // flowStore.classicPolicyScales = output;
  console.log("fetchTMA", output);
  return output;
}

const createTableData = (
  arrangedTerminalData,
  arrangedTerminalPlanData,
  realSectorList,
  realIpiaList
) => {
  let data = new Array();
  // let lastMoment = dayjs(arrangedTerminalData[arrangedTerminalData.length - 1].moment);
  let nowMoment = dayjs();
  for (let i = 0; i < arrangedTerminalData.length; i++) {
    let real = arrangedTerminalData[i];
    let plan = arrangedTerminalPlanData[i];
    let realMoment = dayjs(real.moment);
    let isBefore = nowMoment.isBefore(realMoment);
    // console.log(
    //   nowMoment.format("YYYY-MM-DD HH:mm:ss"),
    //   isBefore,
    //   realMoment.format("YYYY-MM-DD HH:mm:ss")
    // );
    let realSector = realSectorList[i];
    let realIPIA = realIpiaList[i];
    let cell = {};

    cell["时刻"] = plan.moment.substr(6, 2) + "时";
    cell["双流起飞(计划)"] = plan.airport.ZUUU.type.DEP.total;
    cell["双流起飞(实际)"] = isBefore ? "" : real.airport.ZUUU.type.DEP.total;
    cell["双流落地(计划)"] = plan.airport.ZUUU.type.ARR.total;
    cell["双流落地(实际)"] = isBefore ? "" : real.airport.ZUUU.type.ARR.total;
    cell["天府起飞(计划)"] = plan.airport.ZUTF.type.DEP.total;
    cell["天府起飞(实际)"] = isBefore ? "" : real.airport.ZUTF.type.DEP.total;
    cell["天府落地(计划)"] = plan.airport.ZUTF.type.ARR.total;
    cell["天府落地(实际)"] = isBefore ? "" : real.airport.ZUTF.type.ARR.total;
    cell["绵阳起飞(计划)"] = plan.airport.ZUMY.type.DEP.total;
    cell["绵阳起飞(实际)"] = isBefore ? "" : real.airport.ZUMY.type.DEP.total;
    cell["绵阳落地(计划)"] = plan.airport.ZUMY.type.ARR.total;
    cell["绵阳落地(实际)"] = isBefore ? "" : real.airport.ZUMY.type.ARR.total;
    cell["双流合计(计划)"] = plan.airport.ZUUU.total;
    cell["双流合计(实际)"] = isBefore ? "" : real.airport.ZUUU.total;
    cell["天府合计(计划)"] = plan.airport.ZUTF.total;
    cell["天府合计(实际)"] = isBefore ? "" : real.airport.ZUTF.total;
    cell["绵阳合计(计划)"] = plan.airport.ZUMY.total;
    cell["绵阳合计(实际)"] = isBefore ? "" : real.airport.ZUMY.total;
    cell["起飞合计(计划)"] =
      plan.airport.ZUUU.type.DEP.total +
      plan.airport.ZUTF.type.DEP.total +
      plan.airport.ZUMY.type.DEP.total;
    cell["起飞合计(实际)"] = isBefore
      ? ""
      : real.airport.ZUUU.type.DEP.total +
        real.airport.ZUTF.type.DEP.total +
        real.airport.ZUMY.type.DEP.total;
    cell["落地合计(计划)"] =
      plan.airport.ZUUU.type.ARR.total +
      plan.airport.ZUTF.type.ARR.total +
      plan.airport.ZUMY.type.ARR.total;
    cell["落地合计(实际)"] = isBefore
      ? ""
      : real.airport.ZUUU.type.ARR.total +
        real.airport.ZUTF.type.ARR.total +
        real.airport.ZUMY.type.ARR.total;
    cell["合计(计划)"] =
      plan.airport.ZUUU.total +
      plan.airport.ZUTF.total +
      plan.airport.ZUMY.total;
    cell["合计(实际)"] = isBefore
      ? ""
      : real.airport.ZUUU.total +
        real.airport.ZUTF.total +
        real.airport.ZUMY.total;
    cell["预测差值"] = isBefore
      ? ""
      : plan.airport.ZUUU.total +
          plan.airport.ZUTF.total +
          plan.airport.ZUMY.total -
          real.airport.ZUUU.total -
          real.airport.ZUTF.total -
          real.airport.ZUMY.total >
        0
      ? "+" +
        (plan.airport.ZUUU.total +
          plan.airport.ZUTF.total +
          plan.airport.ZUMY.total -
          real.airport.ZUUU.total -
          real.airport.ZUTF.total -
          real.airport.ZUMY.total)
      : plan.airport.ZUUU.total +
        plan.airport.ZUTF.total +
        plan.airport.ZUMY.total -
        real.airport.ZUUU.total -
        real.airport.ZUTF.total -
        real.airport.ZUMY.total;
    cell["建议扇区(计划)"] = plan.sectorRequire;
    cell["建议扇区(实际)"] = isBefore ? "" : real.sectorRequire;
    cell["实开扇区"] = isBefore ? "" : realSector == 0 ? 1 : realSector;
    cell["开扇差值"] = isBefore
      ? ""
      : (realSector == 0 ? 1 : realSector) - real.sectorRequire > 0
      ? "+" + ((realSector == 0 ? 1 : realSector) - real.sectorRequire)
      : (realSector == 0 ? 1 : realSector) - real.sectorRequire;
    // cell['开扇建议依据(实际: !isAfter?'')']=real.sectorReason,
    cell["独立进近(扇建)"] = plan.extInfo
      ? plan.extInfo.standalone
        ? "√"
        : ""
      : "";

    cell["独立进近(实际)"] = plan.extInfo
      ? plan.extInfo.standalone
        ? "√"
        : ""
      : "";
    cell["PMS"] = plan.extInfo ? (plan.extInfo.pms ? "√" : "") : "";
    data.push(cell);
  }
  return data;
};

const createsummaryTerminalData = (
  summaryTerminalData,
  summaryTerminalPlanData,
  isAfter
) => {
  let cellZUUU = {};
  (cellZUUU["机场"] = "双流"),
    (cellZUUU["起飞(计划)"] = summaryTerminalPlanData.airport.ZUUU.type.DEP),
    (cellZUUU["起飞(实际)"] = !isAfter
      ? "待统计"
      : summaryTerminalData.airport.ZUUU.type.DEP),
    (cellZUUU["落地(计划)"] = summaryTerminalPlanData.airport.ZUUU.type.ARR),
    (cellZUUU["落地(实际)"] = !isAfter
      ? "待统计"
      : summaryTerminalData.airport.ZUUU.type.ARR),
    (cellZUUU["合计(计划)"] = summaryTerminalPlanData.airport.ZUUU.total);
  cellZUUU["合计(实际)"] = !isAfter
    ? "待统计"
    : summaryTerminalData.airport.ZUUU.total;
  let cellZUTF = {};
  (cellZUTF["机场"] = "天府"),
    (cellZUTF["起飞(计划)"] = summaryTerminalPlanData.airport.ZUTF.type.DEP),
    (cellZUTF["起飞(实际)"] = !isAfter
      ? "待统计"
      : summaryTerminalData.airport.ZUTF.type.DEP),
    (cellZUTF["落地(计划)"] = summaryTerminalPlanData.airport.ZUTF.type.ARR),
    (cellZUTF["落地(实际)"] = !isAfter
      ? "待统计"
      : summaryTerminalData.airport.ZUTF.type.ARR),
    (cellZUTF["合计(计划)"] = summaryTerminalPlanData.airport.ZUTF.total);
  cellZUTF["合计(实际)"] = !isAfter
    ? "待统计"
    : summaryTerminalData.airport.ZUTF.total;
  let cellZUMY = {};
  (cellZUMY["机场"] = "绵阳"),
    (cellZUMY["起飞(计划)"] = summaryTerminalPlanData.airport.ZUMY.type.DEP),
    (cellZUMY["起飞(实际)"] = !isAfter
      ? "待统计"
      : summaryTerminalData.airport.ZUMY.type.DEP),
    (cellZUMY["落地(计划)"] = summaryTerminalPlanData.airport.ZUMY.type.ARR),
    (cellZUMY["落地(实际)"] = !isAfter
      ? "待统计"
      : summaryTerminalData.airport.ZUMY.type.ARR),
    (cellZUMY["合计(计划)"] = summaryTerminalPlanData.airport.ZUMY.total);
  cellZUMY["合计(实际)"] = !isAfter
    ? "待统计"
    : summaryTerminalData.airport.ZUMY.total;
  let cellALL = {};
  (cellALL["机场"] = "全部"),
    (cellALL["起飞(计划)"] = summaryTerminalPlanData.type.DEP.total);
  cellALL["起飞(实际)"] = !isAfter
    ? "待统计"
    : summaryTerminalData.type.DEP.total;
  cellALL["落地(计划)"] = summaryTerminalPlanData.type.ARR.total;
  cellALL["落地(实际)"] = !isAfter
    ? "待统计"
    : summaryTerminalData.type.ARR.total;
  cellALL["合计(计划)"] = summaryTerminalPlanData.total;
  cellALL["合计(实际)"] = !isAfter ? "待统计" : summaryTerminalData.total;
  let data = new Array(cellZUUU, cellZUTF, cellZUMY, cellALL);
  return data;
};

const createOriginData = (source) => {
  let data = new Array();
  for (let i = 0; i < source.length; i++) {
    let cell = {};
    cell["时刻"] = source[i].moment.substr(11, 2) + "时";
    cell["双流起飞"] = source[i].airport.ZUUU.type.DEP.total;
    cell["双流落地"] = source[i].airport.ZUUU.type.ARR.total;
    cell["天府起飞"] = source[i].airport.ZUTF.type.DEP.total;
    cell["天府落地"] = source[i].airport.ZUTF.type.ARR.total;
    cell["绵阳起飞"] = source[i].airport.ZUMY.type.DEP.total;
    cell["绵阳落地"] = source[i].airport.ZUMY.type.ARR.total;
    cell["MEXAD(天府)"] = source[i].airport.ZUTF.type.ARR.point.MEXAD;
    cell["MEXAD(双流)"] = source[i].airport.ZUUU.type.ARR.point.MEXAD;
    cell["AKDIK(双流)"] = source[i].airport.ZUUU.type.ARR.point.AKDIK;
    cell["BUPMI(天府)"] = source[i].airport.ZUTF.type.ARR.point.BUPMI;
    cell["GURET(双流)"] = source[i].airport.ZUUU.type.DEP.point.GURET;
    cell["SAGPI(天府)"] = source[i].airport.ZUTF.type.DEP.point.SAGPI;
    cell["UBRAB(天府)"] = source[i].airport.ZUTF.type.DEP.point.UBRAB;
    cell["UBRAB(双流)"] = source[i].airport.ZUUU.type.DEP.point.UBRAB;
    cell["EKOKA(双流)"] = source[i].airport.ZUUU.type.ARR.point.EKOKA;
    cell["AKOPI(天府)"] = source[i].airport.ZUTF.type.ARR.point.AKOPI;
    cell["ATVAX(天府)"] = source[i].airport.ZUTF.type.DEP.point.ATVAX;
    cell["IDBOR(双流)"] = source[i].airport.ZUUU.type.DEP.point.IDBOR;
    cell["ELDUD(天府)"] = source[i].airport.ZUTF.type.ARR.point.ELDUD;
    cell["IGNAK(天府)"] = source[i].airport.ZUTF.type.ARR.point.IGNAK;
    cell["IGNAK(双流)"] = source[i].airport.ZUUU.type.ARR.point.IGNAK;
    cell["IGNAK(绵阳)"] = source[i].airport.ZUMY.type.ARR.point.IGNAK;
    cell["LUVEN(天府)"] = source[i].airport.ZUTF.type.DEP.point.LUVEN;
    cell["LUVEN(双流)"] = source[i].airport.ZUUU.type.DEP.point.LUVEN;
    cell["LADUP(天府)"] = source[i].airport.ZUTF.type.ARR.point.LADUP;
    cell["LADUP(双流)"] = source[i].airport.ZUUU.type.ARR.point.LADUP;
    cell["LADUP(绵阳)"] = source[i].airport.ZUMY.type.ARR.point.LADUP;
    cell["MUMGO(天府)"] = source[i].airport.ZUTF.type.DEP.point.MUMGO;
    cell["MUMGO(双流)"] = source[i].airport.ZUUU.type.DEP.point.MUMGO;
    cell["BOKIR(天府)"] = source[i].airport.ZUTF.type.DEP.point.BOKIR;
    cell["BOKIR(双流)"] = source[i].airport.ZUUU.type.DEP.point.BOKIR;
    data.push(cell);
  }
  return data;
};

const transposeMatrixData = (source) => {
  let keys = Object.keys(source[0]);
  let header = new Array(keys[0]);
  for (let i = 0; i < source.length; i++) {
    header.push(source[i][keys[0]]);
  }
  // console.log(header);
  let data = new Array();
  for (let k = 1; k < keys.length; k++) {
    let cell = {};
    cell[header[0]] = keys[k];
    for (let h = 1; h < header.length; h++) {
      cell[header[h]] = source[h - 1][keys[k]];
    }
    data.push(cell);
  }
  return data;
};

// Independent Parallel Instrument Approach

const fetchIpia = (rawTerminalData) => {
  let result = [];
  rawTerminalData.forEach((cell) => {
    if (cell.details && cell.details.isIPIA) {
      result.push(1);
    } else {
      result.push(0);
    }
  });
  return result;
};
